<?php

defined('SYSPATH') or die('No direct access allowed.');

class Controller_CPanel_Users extends Controller_CPanel {

    public function action_index() {

        //Check forms

        switch (Arr::get($_POST, 'action', '')) {
            case 'add_user':
                self::add_user();
                break;
            default:
                break;
        }

        if (isset($_POST['save_user'])) {
            self::save_user();
        } elseif (isset($_POST['remove_user'])) {
            self::remove_user();
        }

        //Render content

        $this->title = __('Users');
        $view = View::factory('cpanel/content/users');

        $per_page = 16;
        $page = max(1, arr::get($_GET, 'page', 1));

        $view->users = ORM::factory('user')->limit($per_page)->offset($per_page * ($page - 1))->find_all();
        $view->pagination = Pagination::factory(array(
                    'total_items' => ORM::factory('user')->count_all(),
                    'items_per_page' => $per_page,
                ));

        $view->roles = ORM::factory('role')->find_all();

        $this->content = $view->render();
    }

    private function add_user() {
        $username = Arr::get($_POST, 'username');
        $email = Arr::get($_POST, 'email');
        $password = Arr::get($_POST, 'password');
        $roles = Arr::get($_POST, 'roles', array());

        $user = ORM::factory('user');
        $user->username = $username;
        $user->email = $email;
        $user->password = $password;

        try {
            $user->save();
        } catch (ORM_Validation_Exception $e) {
            $errors = $e->errors('validation');
            foreach ($errors as $error)
                parent::add_message($error, 'error');
            return;
        }

        foreach ($roles as $role)
            $user->add('roles', ORM::factory('role', $role));
        $user->save();

        parent::add_message('New user created!', 'success');
    }

    private function save_user() {
        $user_id = Arr::get($_POST, 'id');

        if ($user_id == NULL)
            return;

        $user = ORM::factory('user', $user_id);
        $user->email = Arr::get($_POST, 'email');

        $password = Arr::get($_POST, 'password', '');
        if (strlen($password) > 0)
            $user->password = $password;

        try {
            $user->save();
        } catch (ORM_Validation_Exception $e) {
            $errors = $e->errors('validation');
            foreach ($errors as $error)
                parent::add_message($error, 'error');
            return;
        }

        $roles = array_values(Arr::get($_POST, 'roles', array()));

        $all_roles = ORM::factory('role')->find_all();
        foreach ($all_roles as $role) {
            if (in_array($role->id, $roles)) {
                if (!$user->has('roles', $role))
                    $user->add('roles', $role);
            } else {
                if ($user->has('roles', $role))
                    $user->remove('roles', $role);
            }
        }

        parent::add_message('Changes saved!', 'success');
    }

    private function remove_user() {
        $user_id = Arr::get($_POST, 'id');

        if ($user_id == NULL)
            return;

        ORM::factory('user', $user_id)->delete();

        parent::add_message('User removed!', 'success');
    }

}